#include "AlbumWidget.h"
#include "ui_AlbumWidget.h"
#include "albummodel.h"
#include "picturemodel.h"
#include "ThumbnailProxyModel.h"
#include "PictureDelegate.h"

#include <QInputDialog>
#include <QFileDialog>


AlbumWidget::AlbumWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AlbumWidget),
    mAlbumModel(nullptr),
    mAlbumSelectionModel(nullptr),
    mPictureModel(nullptr),
    mPictureSelectionModel(nullptr)
{
    ui->setupUi(this);
    clearUi();

    ui->thumbnailListView->setSpacing(5);
    ui->thumbnailListView->setResizeMode(QListView::Adjust);
    ui->thumbnailListView->setFlow(QListView::LeftToRight);
    ui->thumbnailListView->setWrapping(true);
    ui->thumbnailListView->setItemDelegate(new PictureDelegate(this));

    connect(ui->thumbnailListView, &QListView::doubleClicked, this, &AlbumWidget::pictureActivated);
    connect(ui->addPictureButton, &QPushButton::clicked, this, &AlbumWidget::addPictures);
    connect(ui->editButton, &QPushButton::clicked, this, &AlbumWidget::editAlbum);
    connect(ui->deleteButton, &QPushButton::clicked, this, &AlbumWidget::deleteAlbum);
}

AlbumWidget::~AlbumWidget()
{
    delete ui;
}

void AlbumWidget::setAlbumModel(AlbumModel *albumModel)
{
    mAlbumModel = albumModel;
    connect(mAlbumModel, &QAbstractItemModel::dataChanged, [this](const QModelIndex &topLeft) {
        if( topLeft == mAlbumSelectionModel->currentIndex()) {
            loadAlbum(topLeft);
        }
    });
}

void AlbumWidget::setAlbumSelectionModel(QItemSelectionModel *albumSelectionModel)
{
    mAlbumSelectionModel = albumSelectionModel;
    connect(mAlbumSelectionModel, &QItemSelectionModel::selectionChanged, [this] (const QItemSelection& selected) {
        if( selected.isEmpty()) {
            clearUi();
            return;
        }
        loadAlbum(selected.indexes().first());
    });
}

void AlbumWidget::setPictureModel(ThumbnailProxyModel *pictureModel)
{
    mPictureModel = pictureModel;
    ui->thumbnailListView->setModel(mPictureModel);
}

void AlbumWidget::setPictureSelectionModel(QItemSelectionModel *selectionModel)
{
    mPictureSelectionModel = selectionModel;
    ui->thumbnailListView->setSelectionModel(mPictureSelectionModel);
}

void AlbumWidget::deleteAlbum()
{
    if(mAlbumSelectionModel->selectedIndexes().isEmpty()) {
        return;
    }

    int row = mAlbumSelectionModel->currentIndex().row();
    mAlbumModel->removeRow(row);

    // Try to select the previous album
    QModelIndex previousModelIndex = mAlbumModel->index(row-1, 0);
    if(previousModelIndex.isValid()) {
        mAlbumSelectionModel->setCurrentIndex(previousModelIndex,
                                              QItemSelectionModel::SelectCurrent);
        return;
    }

    // Try to select the next album
    QModelIndex nextModelIndex = mAlbumModel->index(row, 0);
    if(nextModelIndex.isValid()) {
        mAlbumSelectionModel->setCurrentIndex(nextModelIndex,
                                              QItemSelectionModel::SelectCurrent);
        return;
    }
}

void AlbumWidget::editAlbum()
{
    if(mAlbumSelectionModel->selectedIndexes().isEmpty()) {
        return;
    }

    QModelIndex currentAlbumIndex = mAlbumSelectionModel->selectedIndexes().first();
    QString oldAlbumName = mAlbumModel
            ->data(currentAlbumIndex, AlbumModel::NameRole)
            .toString();

    bool ok;
    QString newName = QInputDialog::getText(
                this,
                "Album's name",
                "Change Album name",
                QLineEdit::Normal,
                oldAlbumName,
                &ok);
    if( ok && !newName.isEmpty() ) {
        mAlbumModel->setData(
                    currentAlbumIndex,
                    newName,
                    AlbumModel::NameRole);
    }
}

void AlbumWidget::addPictures()
{
    QStringList filenames = QFileDialog::getOpenFileNames(
                this,
                "Add pictures",
                QDir::homePath(),
                "Picture file (*.jpg *.png)");

    if( !filenames.isEmpty() ) {
        QModelIndex lastModelIndex;
        for(auto filename: filenames) {
            Picture picture(filename);
            lastModelIndex = mPictureModel->pictureModel()->addPicture(picture);
        }
        ui->thumbnailListView->setCurrentIndex(lastModelIndex);
    }
}

void AlbumWidget::clearUi()
{
    ui->albumName->setText("");
    ui->deleteButton->setVisible(false);
    ui->editButton->setVisible(false);
    ui->addPictureButton->setVisible(false);
}

void AlbumWidget::loadAlbum(const QModelIndex &albumIndex)
{
    int albumId = mAlbumModel->data(albumIndex, AlbumModel::IdRole).toInt();
    QString albumName = mAlbumModel->data(albumIndex, AlbumModel::NameRole).toString();

    mPictureModel->pictureModel()->setAlbumId(albumId);
    ui->albumName->setText(albumName);
    ui->deleteButton->setVisible(true);
    ui->editButton->setVisible(true);
    ui->addPictureButton->setVisible(true);
}
